package com.healthbox.appointmentservice.controller;

import com.healthbox.appointmentservice.DTO.HospitalDTO;
import com.healthbox.appointmentservice.common.Result;
import com.healthbox.appointmentservice.entity.Hospital;
import com.healthbox.appointmentservice.service.HospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pengpeng
 * @date 2022/5/8
 */
@RestController
@RequestMapping(path = "/organs")
@Slf4j
@Api(tags = "检测机构管理")
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;

    @ApiOperation(value = "添加新检测机构", notes = "只有系统管理员具有权限，类似于与某家检查机构达成合作关系后录入检测机构信息")
    @PostMapping
    public Result<Hospital> newHospital(@RequestBody HospitalDTO hospitalDTO){
        log.info("添加检测机构" + hospitalDTO);
        Hospital hospital = hospitalService.findOne(hospitalDTO.getPhone());
        if (hospital!=null) {
            log.warn("注册机机构手机号冲突: \nnew:" + hospitalDTO + "\nold:" + hospital);
            return Result.badRequest("该手机号已被使用");
        }
        hospital = hospitalService.newHospital(hospitalDTO);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "修改检测机构信息", notes = "只有系统管理员具有权限,检测机构不能自己修改信息，需要线下通知管理员来修改")
    @PutMapping()
    public Result<Hospital> updateHospital(@RequestBody HospitalDTO hospitalDTO) {
        log.info("修改机构" + hospitalDTO);
        Hospital hospital = hospitalService.findOne(hospitalDTO.getPhone());
        if (hospital==null) {
            log.warn("该机构不存在: \nnew:" + hospitalDTO);
            return Result.badRequest("修改的机构不存在");
        }
        hospital = hospitalService.updateHospital(hospitalDTO);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "查询所有检测机构", notes = "获取所有检测机构,默认搜索全部，可以通过检测机构名称和手机号搜索，支持模糊查询(使用like可以少输入没关系，输错就找不到)")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "检测机构名称", type = "String", required = false),
        @ApiImplicitParam(name = "phone", value = "检测机构手机号", type = "String", required = false)
    })
    @GetMapping
    public Result<List<Hospital>> hospitals(@RequestParam(required = false) String name, @RequestParam(required = false) String phone) {
        return Result.ok(hospitalService.findHospital(name, phone));
    }
}
